<template>
  <el-container>
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt />
        <span>后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse?'64px':'200px'">
        <div class="toggle-button" @click='toggleCollapse'>|||</div>
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="iscollapse" :collapse-transition='false' :router="true" :default-active="defaultactive">
          <!-- 可以展开的一级菜单 -->
          <el-submenu :index="item.order+''" v-for="item in menusList" :key="item.id" >
            <!-- 定义一级菜单展示效果 -->
            <template slot="title">
              <i :class="iconsObj[item.order]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children" :key="subitem.id" @click='setSesstionActive("/"+subitem.path)'>
              <template slot="title">
              <i class="el-icon-menu"></i>
              <span slot="title">{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
          
        </el-menu>
      </el-aside>
      <el-main>
        <transition>
           <router-view></router-view>
        </transition>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menusList:[],
      iconsObj:{'1':'iconfont icon-user','2':'iconfont icon-tijikongjian','3':'iconfont icon-shangpin','4':'iconfont icon-danju','5':'iconfont icon-baobiao'},
      iscollapse:false,//是否折叠
      defaultactive:''//高亮激活地址
    };
  },
  created() {
    this.getMenuList();
    this.defaultactive=sessionStorage.getItem('active')||'/users';
  },

  methods: {
    //退出事件
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList(){
     const {data:res}= await this.$axios.get('menus')
     if(res.meta.status!==200)return this.$message.error('获取数据失败')
     this.menusList=res.data;
     
     
    },
    // 点击按钮切换菜单折叠和展开
    toggleCollapse(){
      this.iscollapse=!this.iscollapse;
    },
    // 保存链接的激活状态
    setSesstionActive(path){
      sessionStorage.setItem('active',path);
      this.defaultactive=path;
      
    }
  }
};
</script>

<style scoped lang="less">
.el-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .logo {
    display: flex;
    align-items: center;
    span {
      font-size: 24px;
      color: #fff;
      margin-left: 10px;
    }
  }
}
.el-aside {
  background: #373d41;
  .el-menu{
    border-right: none;
  }
  .toggle-button{
    background: #4a5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
  }
}
.el-main {
  background: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
.v-enter{
  transform: translateX(50px);
}
.v-enter-active{
  transition: all .5s;
}
.v-enter-to{
  transform: translateX(0);
}
</style>
